package com.xr.miniprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xr.common.utils.StringUtils;
import com.xr.common.utils.bean.BeanUtils;
import com.xr.miniprogram.domain.MiniProgramMenu;
import com.xr.miniprogram.domain.MiniProgramService;
import com.xr.miniprogram.domain.MiniProgramStandards;
import com.xr.miniprogram.domain.vo.MiniProgramMenuTree;
import com.xr.miniprogram.mapper.MiniProgramMenuMapper;
import com.xr.miniprogram.mapper.MiniProgramServiceMapper;
import com.xr.miniprogram.mapper.MiniProgramStandardsMapper;
import com.xr.miniprogram.service.IMiniProgramMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.xr.common.constant.MiniProgramConstants;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class IMiniProgramMenuServiceImpl extends ServiceImpl<MiniProgramMenuMapper, MiniProgramMenu> implements IMiniProgramMenuService {
    @Resource
    MiniProgramMenuMapper menuMapper;
    @Resource
    MiniProgramStandardsMapper standardsMapper;
    @Resource
    MiniProgramServiceMapper serviceMapper;
    @Override
    public List<MiniProgramMenu> getMenuList() {
        return this.list();
    }

    @Override
    public List<MiniProgramMenuTree> getMenuTreeList() {
        return this.buildMenuTree(this.getMenuList());
    }

    /**
     * 列表构建树形数据
     * @param miniProgramMenus
     * @return
     */
    public List<MiniProgramMenuTree> buildMenuTree(List<MiniProgramMenu> miniProgramMenus) {
        return miniProgramMenus.stream().filter(e -> MiniProgramConstants.MENU_LEAVE_1.equals(e.getMenuLeave())).map(leve1 -> {
            MiniProgramMenuTree miniProgramMenuTree = new MiniProgramMenuTree();
            BeanUtils.copyBeanProp(miniProgramMenuTree, leve1);
            List<MiniProgramMenuTree> collect = miniProgramMenus.stream().filter(son -> son.getMenuLeave().equals(MiniProgramConstants.MENU_LEAVE_2) && son.getParentId().equals(leve1.getMenuId())).map(MiniProgramMenu::toMiniProgramMenuTree).collect(Collectors.toList());
            miniProgramMenuTree.setChildren(collect);
            return miniProgramMenuTree;
        }).collect(Collectors.toList());
    }

    @Override
    public MiniProgramMenu getMenuById(Long menuId) {
        return this.getById(menuId);
    }


    @Override
    public boolean checkServiceNameUnique(MiniProgramMenu miniProgramMenu) {
        Long menuId = StringUtils.isNull(miniProgramMenu.getMenuId()) ? -1L : miniProgramMenu.getMenuId();
        //查询同级别下是否有同名的（同一个父级别下所有的子级别是否有同名的）
        MiniProgramMenu programMenu = this.getOne(new QueryWrapper<MiniProgramMenu>().lambda()
                .eq(MiniProgramMenu::getParentId, miniProgramMenu.getParentId())
                .eq(MiniProgramMenu::getMenuName, miniProgramMenu.getMenuName()));
        //有同名的且id不一样 返回false表示不可用
        return !StringUtils.isNotNull(programMenu) || programMenu.getMenuId().equals(menuId);
    }

    @Override
    public boolean addMenu(MiniProgramMenu miniProgramMenu) {
        return this.save(miniProgramMenu);
    }

    @Override
    public boolean updateMenu(MiniProgramMenu miniProgramMenu) {
        return this.updateById(miniProgramMenu);
    }

    @Override
    public boolean deleteMenu(Long menuId) {
        //情况一： 删除一级菜单 其下二级菜单、二级菜单所含服务、服务所含规格
        //情况二： 删除二级菜单 其下所含服务、服务所含规格
        MiniProgramMenu miniProgramMenu = menuMapper.selectById(menuId);
        Long parentId = miniProgramMenu.getParentId();
        if (parentId != 0) {
            MiniProgramService miniProgramService = serviceMapper.selectOne(new QueryWrapper<MiniProgramService>().eq("menu_id", menuId));
            if (miniProgramService != null) {
                Long serviceId = miniProgramService.getServiceId();
                MiniProgramStandards miniProgramStandards = standardsMapper.selectById(serviceId);
                if (miniProgramStandards != null) {
                    standardsMapper.deleteById(serviceId);
                    menuMapper.deleteMenu(menuId);
                }
                return menuMapper.deleteMenu(menuId);
            }
            return menuMapper.deleteByMenuId(menuId);
        } else {
            List<MiniProgramMenu> childMenu = menuMapper.selectList(new QueryWrapper<MiniProgramMenu>().eq("parent_id", menuId));
            for (MiniProgramMenu menu : childMenu) {
                Long menuId1 = menu.getMenuId();
                MiniProgramService miniProgramService1 = serviceMapper.selectOne(new QueryWrapper<MiniProgramService>().eq("menu_id", menuId1));
                //判断服务是否为空
                if (miniProgramService1 != null) {
                    //服务不为空，查询服务的规格
                    Long serviceId1 = miniProgramService1.getServiceId();
                    MiniProgramStandards miniProgramStandards = standardsMapper.selectById(serviceId1);
                    //判断规格是否为空
                    if (miniProgramStandards != null) {
                        //规格不为空，直接删除规格,然后再删除服务和菜单
                        standardsMapper.deleteById(serviceId1);
                        menuMapper.deleteMenu(menuId1);
                    }
                    //如果规格为空.则只删除服务和菜单
                    return menuMapper.deleteMenu(menuId1);
                }
                //如果服务为空，则只删除菜单
                menuMapper.deleteByMenuId(menuId1);
            }
            //最后删除要删除的菜单
            return menuMapper.deleteByMenuId(menuId);
        }
    }
}
